package common

import (
	"context"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common/openapi_dto"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/repository/common"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
)

type CreateTenantService struct {
	createTenantRepository common.CreateTenantReposIface
}

func NewCreateTenantService(
	createTenantRepository common.CreateTenantReposIface,
) CreateTenantServiceIface {
	return &CreateTenantService{
		createTenantRepository: createTenantRepository,
	}
}
func (s *CreateTenantService) CreateTenant(ctx context.Context, request *dto.CreateTenantRequest) (*dto.CreateTenantResponse, error) {
	//参数转换
	requestRep := &pb.CreateTenantRequest{}
	requestRep.TaskId = request.TaskId
	requestRep.DeliveryNo = request.DeliveryNo
	requestRep.DeliveryBatchNo = request.DeliveryBatchNo
	requestRep.EnterpriseId = request.EnterpriseId
	requestRep.EnterpriseName = request.EnterpriseName
	requestRep.EnterpriseCode = request.EnterpriseCode
	requestRep.TenantCode = request.TenantCode
	requestRep.TenantName = request.TenantName
	requestRep.CustomerId = request.CustomerId
	requestRep.CustomerCode = request.CustomerCode
	requestRep.CustomerName = request.CustomerName
	requestRep.CustomerShortName = request.CustomerShortName
	requestRep.TopCustomerId = request.TopCustomerId
	requestRep.TopCustomerName = request.TopCustomerName
	requestRep.CustomerCorpId = request.CustomerCorpId
	requestRep.CustomerCorpName = request.CustomerCorpName
	requestRep.MasterId = request.MasterId
	requestRep.MasterUserName = request.MasterUserName
	requestRep.MasterName = request.MasterName
	requestRep.MasterPhone = request.MasterPhone
	requestRep.MasterEmail = request.MasterEmail
	requestRep.EnvType = request.EnvType
	requestRep.IsSpecialApproval = request.IsSpecialApproval
	requestRep.SpecialApprovalType = request.SpecialApprovalType
	requestRep.SpecialType = request.SpecialType
	requestRep.LastModificationTime = request.LastModificationTime
	requestRep.CreationTime = request.CreationTime
	requestRep.InitUserName = request.InitUserName
	requestRep.InitPassword = request.InitPassword
	//合同
	if len(request.Contracts) > 0 {
		for _, v := range request.Contracts {
			requestRep.Contracts = append(requestRep.Contracts, &pb.Contracts{
				Id:                v.Id,
				Code:              v.Code,
				SignTime:          v.SignTime,
				AccountCreateTime: v.AccountCreateTime,
				DeliveryUserEmail: v.DeliveryUserEmail,
				CorpId:            v.CorpId,
				CorpName:          v.CorpName,
				Summary:           v.Summary,
				SummaryText:       v.SummaryText,
			})
		}
	}
	//权益
	if len(request.AppAuths) > 0 {
		for _, m := range request.AppAuths {
			appAuths := &pb.AppAuths{
				AppId:                 m.AppId,
				AppName:               m.AppName,
				DeliveryNo:            m.DeliveryNo,
				ProductId:             m.ProductId,
				ProductCode:           m.ProductCode,
				ProductName:           m.ProductName,
				RelationId:            m.RelationId,
				RelationIdType:        m.RelationIdType,
				AppCode:               m.AppCode,
				AppType:               m.AppType,
				AppEffectiveStartTime: m.AppEffectiveStartTime,
				AppEffectiveEndTime:   m.AppEffectiveEndTime,
				ErpUserCount:          m.ErpUserCount,
				//MainProductCode:       m.MainProductCode,
				MainProductId:   m.MainProductId,
				MainProductName: m.MainProductName,
				IsFree:          m.IsFree,
				IsPublic:        m.IsPublic,
				ContractCode:    m.ContractCode,
			}
			for _, mm := range m.Auths {
				appAuths.Auths = append(appAuths.Auths, &pb.Auths{
					AuthId:               mm.AuthID,
					LastModificationTime: mm.LastModificationTime,
					CreationTime:         mm.CreationTime,
					SignType:             mm.SignType,
					AuthNum:              mm.AuthNum,
					MaxNum:               mm.MaxNum,
					AuthStandard:         mm.AuthStandard,
					AuthPeriod:           mm.AuthPeriod,
					AuthTypeName:         mm.AuthTypeName,
					AuthTypeCode:         mm.AuthTypeCode,
					EffectiveStartTime:   mm.EffectiveStartTime,
					EffectiveEndTime:     mm.EffectiveEndTime,
					Projects:             mm.Projects,
				})
			}
			requestRep.AppAuths = append(requestRep.AppAuths, appAuths)
		}
	}
	authorization := ""
	if ctx.Value("Authorization") != nil {
		authorization = ctx.Value("Authorization").(string)
	}
	requestRep.Authorization = authorization

	resp := &dto.CreateTenantResponse{}
	data, err := s.createTenantRepository.CreateTenant(ctx, requestRep)
	if err != nil {
		return resp, err
	}
	if data.Success == true {
		resp.Code = 0
	} else {
		resp.Code = 4000
		if err != nil {
			resp.Message = err.Error()
		} else {
			resp.Message = ""
		}

	}
	return resp, nil
}

func (s *CreateTenantService) RemoveTenant(ctx context.Context, request *dto.RemoveTenantRequest) (*dto.RemoveTenantResponse, error) {
	requestRep := &pb.RemoveTenantRequest{
		TaskId:          request.TaskId,
		TaskCallbackUrl: request.TaskCallbackUrl,
		EnterpriseCode:  request.EnterpriseCode,
		EnterpriseName:  request.EnterpriseName,
		TenantCode:      request.TenantCode,
		TenantName:      request.TenantName,
		CustomerId:      request.CustomerId,
	}
	authorization := ""
	if ctx.Value("Authorization") != nil {
		authorization = ctx.Value("Authorization").(string)
	}
	requestRep.Authorization = authorization

	resp := &dto.RemoveTenantResponse{}
	data, err := s.createTenantRepository.RemoveTenant(ctx, requestRep)
	if err != nil {
		return resp, err
	}
	if data.Success == true {
		resp.Code = 0
	} else {
		resp.Code = 4000
		if err != nil {
			resp.Message = err.Error()
		} else {
			resp.Message = ""
		}
	}
	return resp, nil
}

func (s *CreateTenantService) GetTaskStatus(ctx context.Context, request *dto.GetTaskStatusRequest) (interface{}, error) {
	requestRep := &pb.GetTaskStatusRequest{
		TaskId: request.TaskId,
	}
	authorization := ""
	if ctx.Value("Authorization") != nil {
		authorization = ctx.Value("Authorization").(string)
	}
	requestRep.Authorization = authorization

	resp := &dto.GetTaskStatusSuccessResponse{}
	data, err := s.createTenantRepository.GetTaskStatus(ctx, requestRep)
	if err != nil {
		return resp, err
	}
	resp.Code = 0
	resp.Data = dto.GetTaskStatus{
		TaskStatus:            data.TaskStatus,
		TaskStatusDescription: data.TaskStatusDescription,
	}
	return resp, nil
}

func (s *CreateTenantService) CreateTenantCallback(ctx context.Context, request *dto.CreateTenantCallbackRequest) (*dto.CreateTenantCallbackResponse, error) {
	requestRep := &pb.CreateTenantCallbackRequest{
		TaskId: request.TaskId,
	}
	resp := &dto.CreateTenantCallbackResponse{}
	data, err := s.createTenantRepository.CreateTenantCallback(ctx, requestRep)
	if err != nil {
		return resp, err
	}
	resp.TaskStatus = data.TaskStatus
	resp.TaskStatusDescription = data.TaskStatusDescription
	return resp, nil
}
